﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeadExpress.Models;
using LeadExpress.Models.ViewPages;
using System.Web.Mvc;
using System.Data;

namespace LeadExpress.Repository
{
    public class ReportProblemManagementRepository : BaseManagementRepository
    {
        LeadExpressEntities entities = new LeadExpressEntities();

        public ReportProblemManagementModel GetModel(string pageType, string roleName, int id)
        {
            ReportProblemManagementModel model = new ReportProblemManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListReportProblem = GetAll();

            if (id > 0)
            {
                model.ProblemInfo = GetSingle(id);
            }

            return model;
        }

        public ReportProblemManagementModel GetModel(string pageType, string roleName)
        {
            ReportProblemManagementModel model = new ReportProblemManagementModel();
            model.ListReportProblem = GetAll();

            return model;
        }

        public List<ReportProblemModel> GetAll()
        {
            List<ReportProblemModel> list = new List<ReportProblemModel>();

            var items = (from c in entities.ReportProblems
                         orderby c.DateCreated descending
                         select c).ToList();

            foreach (var item in items)
            {
                ReportProblemModel model = new ReportProblemModel();

                model.ID = item.ID;
                model.Fullname = item.Fullname;
                model.EmailAddress = item.EmailAddress;
                model.Phone = item.Phone;
                model.LinkUrl = item.LinkURL;
                model.Status = (item.Approved) ? "Đã xử lý" : "Mới";
                model.DateCreated = Convert.ToDateTime(item.DateCreated).ToString("dd-MM-yyyy hh:mm tt");
                model.Approved = item.Approved;
                model.Active = item.Active;
                list.Add(model);
            }

            return list;
        }

        #region Export CSV
        public FileResult ExportToCSVFile()
        {
            var list = GetAllProblem();
            DataTable dt = CreateDataTableReturnLeads(list);
            return CreateCSVFile(dt, "ProblemReport");
        }

        private List<ReportProblemModel> GetAllProblem()
        {
            List<ReportProblemModel> list = new List<ReportProblemModel>();

            var items = (from c in entities.ReportProblems
                         orderby c.DateCreated descending
                         select c).ToList();

            foreach (var item in items)
            {
                ReportProblemModel model = new ReportProblemModel();

                model.ID = item.ID;
                model.Fullname = item.Fullname;
                model.EmailAddress = item.EmailAddress;
                model.Phone = item.Phone;
                model.LinkUrl = item.LinkURL;
                model.Comment = item.Comment;
                model.Status = (item.Approved) ? "Đã xử lý" : "Mới";
                model.Approved = item.Approved;
                model.Active = item.Active;
                list.Add(model);
            }

            return list;
        }

        private static DataTable CreateDataTableReturnLeads(List<ReportProblemModel> data)
        {
            // Here we create a DataTable with columns.
            DataTable table = new DataTable();
            table.Columns.Add("Tên đầy đủ", typeof(string));
            table.Columns.Add("Địa chỉ email", typeof(string));
            table.Columns.Add("Link", typeof(string));
            table.Columns.Add("Nhận xét", typeof(string));
            table.Columns.Add("Mã", typeof(string));
            table.Columns.Add("Phone", typeof(string));
            table.Columns.Add("Ngày tạo", typeof(string));
            table.Columns.Add("Tình trạng", typeof(string));

            // Here we add DataRows.
            if (data.Count > 0)
            {
                foreach (var item in data)
                {
                    table.Rows.Add(item.Fullname, item.EmailAddress, item.LinkUrl, item.Comment, item.ID, item.Phone, item.DateCreated, item.Status);
                }
            }
            return table;
        }
        #endregion

        public ReportProblem GetSingle(int id)
        {
            return (from _object in entities.ReportProblems where _object.ID == id select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] ReportProblem _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToReportProblems(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.ReportProblems where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] ReportProblem _object)
        {
            _object.EntityKey = (from ReportProblem ac in entities.ReportProblems where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}